package com.smallkingson.ylib.view;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.smallkingson.ylib.R;


public class YCircleImageView extends android.support.v7.widget.AppCompatImageView {
	private int width;
	private int height;
	private Bitmap bitmap;
	private Paint paint;
	private int borderWidth = 3;
	private int borderCoror = 0xffcccccc;
	private Path path;
	PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
	RectF rectF = new RectF();
	@SuppressLint("NewApi")
	public YCircleImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		TypedArray array = context
				.obtainStyledAttributes(attrs, R.styleable.yr);
		borderCoror = array.getColor(R.styleable.yr_borderColor,0xffcccccc);
		borderWidth = array.getInteger(R.styleable.yr_borderWidth_y, 1);
		path = new Path();
//		setScaleType(ScaleType.CENTER_CROP);
		setLayerType(View.LAYER_TYPE_HARDWARE, null);
		setLayerType(View.LAYER_TYPE_SOFTWARE, null);
	}

	public YCircleImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public YCircleImageView(Context context) {
		this(context, null);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		width = getWidth();
		height = getHeight();

		Drawable drawable = getDrawable();
		if (drawable instanceof BitmapDrawable) {

			bitmap = ((BitmapDrawable) drawable).getBitmap();
		}else{
			return ;
		}

		paint.setStyle(Paint.Style.FILL);
		paint.setShader(null);
		paint.setAntiAlias(true);

//		int sc = canvas.saveLayer(0, 0, width, height, null,
//				Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
//						| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
//						| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
//						| Canvas.CLIP_TO_LAYER_SAVE_FLAG);

		path.reset();
		if (width >= height) {
			rectF.set((width - height) / 2, 0, (width + height) / 2,
					height);
		} else {
			rectF.set(0, (height - width) / 2, width,
					(height + width) / 2);
		}
		path.addOval(rectF, Path.Direction.CCW);
		canvas.drawPath(path, paint);

		paint.setXfermode(xfermode);
		// YCanvases.draw(canvas, bitmap, 0, 0, width, height, paint);
		// 画bitmap
		canvas.drawBitmap(bitmap, getImageMatrix(), paint);
		// 画边框
		path.reset();
		path.addOval(rectF, Path.Direction.CCW);
		paint.setColor(borderCoror);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(borderWidth);
		canvas.drawPath(path, paint);

		paint.setXfermode(null);

//		canvas.restoreToCount(sc);
	}
}